/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fplaton.ui;

import fplaton.db.SQLiteJDBC;
import fplaton.helper.InsertClassHelper;
import java.awt.Component;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


/**
 *
 * @author evanmana
 *    
 */
public class InsertClass extends javax.swing.JFrame {

    /**
     * Creates new form InsertClass
     */
    public InsertClass() {
        initComponents();
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabelClass = new javax.swing.JLabel();
        jLabelCourse = new javax.swing.JLabel();
        jLabelTeacher = new javax.swing.JLabel();
        jLabelTime = new javax.swing.JLabel();
        jLabelDate = new javax.swing.JLabel();
        jComboBoxDay = new javax.swing.JComboBox();
        jComboBoxClass = new javax.swing.JComboBox();
        jTextFieldTeacherName = new javax.swing.JTextField();
        jLabelTitle = new javax.swing.JLabel();
        jComboBoxStartTime = new javax.swing.JComboBox();
        jComboBoxLastTime = new javax.swing.JComboBox();
        jLabelDash = new javax.swing.JLabel();
        jButtonInsertData = new javax.swing.JButton();
        jLabelNumOfStudents = new javax.swing.JLabel();
        jTextFieldStudentNum = new javax.swing.JTextField();
        jComboBoxCourse = new javax.swing.JComboBox();
        jButtonExitInsertClassForm = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Εισαγωγή τμήματος");

        jLabelClass.setText("Τάξη:");

        jLabelCourse.setText("Μάθημα:");

        jLabelTeacher.setText("Καθηγητής:");

        jLabelTime.setText("Ώρα:");

        jLabelDate.setText("Μέρα:");

        jComboBoxDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Δευτέρα", "Τρίτη", "Τετάρτη", "Πέμπτη", "Παρασκευή", "Σάββατο" }));

        jComboBoxClass.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "A' Γυμνασίου", "Β' Γυμνασίου", "Γ' Γυμνασίου", "Α' Λυκείου", "Β' Λυκείου", "Γ' Λυκείου" }));
        jComboBoxClass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxClassActionPerformed(evt);
            }
        });

        jTextFieldTeacherName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldTeacherNameActionPerformed(evt);
            }
        });

        jLabelTitle.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelTitle.setText("Εσαγωγή νέου τμήματος");

        jComboBoxStartTime.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "08.00", "09.00", "10.00", "11.00", "12.00", "13.00", "14.00", "15.00", "16.00", "17.00", "18.00", "19.00", "20.00" }));

        jComboBoxLastTime.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "09.00", "10.00", "11.00", "12.00", "13.00", "14.00", "15.00", "16.00", "17.00", "18.00", "19.00", "20.00", "21.00" }));

        jLabelDash.setText(" -");

        jButtonInsertData.setText("Εισαγωγή");
        jButtonInsertData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonInsertDataActionPerformed(evt);
            }
        });

        jLabelNumOfStudents.setText("Ατομα:");

        jComboBoxCourse.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Αρχαία", "Γεωμετρία", "Γλώσσα", "Ιστορία", "Κείμενα Λογοτεχνίας", "Μαθηματικά", "Οδύσσεια", "Φυσική" }));
        jComboBoxCourse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxCourseActionPerformed(evt);
            }
        });

        jButtonExitInsertClassForm.setText("Έξοδος");
        jButtonExitInsertClassForm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExitInsertClassFormActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabelTitle)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelTeacher)
                                    .addComponent(jLabelNumOfStudents))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldStudentNum)
                                    .addComponent(jTextFieldTeacherName)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelClass)
                                    .addComponent(jLabelCourse)
                                    .addComponent(jLabelDate)
                                    .addComponent(jLabelTime))
                                .addGap(28, 28, 28)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jComboBoxClass, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jComboBoxStartTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabelDash, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jComboBoxLastTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jComboBoxCourse, 0, 192, Short.MAX_VALUE)
                                    .addComponent(jComboBoxDay, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jButtonInsertData)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonExitInsertClassForm, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(30, 30, 30))))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButtonExitInsertClassForm, jButtonInsertData});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabelTitle)
                .addGap(23, 23, 23)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelClass)
                    .addComponent(jComboBoxClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCourse)
                    .addComponent(jComboBoxCourse))
                .addGap(23, 23, 23)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDate)
                    .addComponent(jComboBoxDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelTime)
                    .addComponent(jComboBoxStartTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxLastTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDash))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelTeacher)
                    .addComponent(jTextFieldTeacherName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNumOfStudents)
                    .addComponent(jTextFieldStudentNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonExitInsertClassForm)
                    .addComponent(jButtonInsertData))
                .addGap(19, 19, 19))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(44, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonExitInsertClassFormActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExitInsertClassFormActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButtonExitInsertClassFormActionPerformed

    private void jComboBoxCourseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxCourseActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxCourseActionPerformed

    private void jButtonInsertDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInsertDataActionPerformed
        // TODO add your handling code here:

        Component frame = null;
        String course, yclass, day, starts, ends, teacher_name,  num_of_students;
        Double start_time, last_time;
        int flag=1;

        start_time = Double.valueOf((String)jComboBoxStartTime.getSelectedItem());
        last_time = Double.valueOf((String)jComboBoxLastTime.getSelectedItem());

        course= jComboBoxCourse.getSelectedItem().toString();
        yclass= jComboBoxClass.getSelectedItem().toString();
        day= jComboBoxDay.getSelectedItem().toString();
        starts= jComboBoxStartTime.getSelectedItem().toString();
        ends= jComboBoxLastTime.getSelectedItem().toString();
        teacher_name= jTextFieldTeacherName.getText().toString();
        num_of_students= jTextFieldStudentNum.getText().toString();

                 
        if(InsertClassHelper.CorrectData(start_time, last_time, teacher_name, num_of_students) == true ){  
            //Insert to DATABASE
            try {
                SQLiteJDBC connection = new SQLiteJDBC("frontistirio.db");

                String create_table = "CREATE TABLE IF NOT EXISTS CLASS "
                + "(ID INTEGER PRIMARY KEY  AUTOINCREMENT,"
                + " CLASS     TEXT    NOT NULL,"
                + " COURSE    TEXT    NOT NULL,"
                + " DAY       TEXT    NOT NULL,"
                + " STARTS    TEXT   NOT NULL,"
                + " ENDS      TEXT   NOT NULL,"
                + " TEACHER   TEXT    NOT NULL,"
                + " NUMOFSTUDENTS INTEGER )";
                connection.execute(create_table); // create db table

                // Add to database (INSERT query with formatted strings)
                PreparedStatement prep = connection.conn.prepareStatement(
                    "INSERT INTO CLASS VALUES(?,?, ?, ?, ?, ?, ?, ?);");
                prep.setString(2, yclass);
                prep.setString(3, course);
                prep.setString(4, day);
                prep.setString(5, starts);
                prep.setString(6, ends);
                prep.setString(7, teacher_name);
                prep.setString(8, num_of_students);

                connection.conn.setAutoCommit(false);
                prep.execute();
                connection.conn.setAutoCommit(true);
                
                JOptionPane.showMessageDialog(frame,"Το τμήμα δημιουργήθηκε.");
               
            } catch (SQLException ex) {
                Logger.getLogger(InsertClass.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(frame, "Υπήρξε ένα σφάλμα κατα την εισαγωγή νέου τμήματος.", "Εισαγωγή νέου τμήματος", JOptionPane.ERROR);
            }
            
        }
       
    }//GEN-LAST:event_jButtonInsertDataActionPerformed

    private void jTextFieldTeacherNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldTeacherNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldTeacherNameActionPerformed

    private void jComboBoxClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxClassActionPerformed
        // TODO add your handling code here:

        String a;
        a=jComboBoxClass.getSelectedItem().toString();

        if( a == "A' Γυμνασίου"){

            jComboBoxCourse.removeAllItems();
            // now add back relevant values
            jComboBoxCourse.addItem("Αρχαία");
            jComboBoxCourse.addItem("Γεωμετρία");
            jComboBoxCourse.addItem("Γλώσσα");
            jComboBoxCourse.addItem("Ιστορία");
            jComboBoxCourse.addItem("Κείμενα Λογοτεχνίας");
            jComboBoxCourse.addItem("Μαθηματικά");
            jComboBoxCourse.addItem("Οδύσσεια");
            jComboBoxCourse.addItem("Φυσική");
        }
        else if (a == "Β' Γυμνασίου"){

            jComboBoxCourse.removeAllItems();
            // now add back relevant values
            jComboBoxCourse.addItem("Αρχαία");
            jComboBoxCourse.addItem("Γεωμετρία");
            jComboBoxCourse.addItem("Γλώσσα");
            jComboBoxCourse.addItem("Ιλιάδα");
            jComboBoxCourse.addItem("Ιστορία");
            jComboBoxCourse.addItem("Κείμενα Λογοτεχνίας");
            jComboBoxCourse.addItem("Μαθηματικά");
            jComboBoxCourse.addItem("Οδύσσεια");
            jComboBoxCourse.addItem("Φυσική");
            jComboBoxCourse.addItem("Χημεία");
        }

        else if (a == "Γ' Γυμνασίου"){

            jComboBoxCourse.removeAllItems();
            // now add back relevant values
            jComboBoxCourse.addItem("Αρχαία");
            jComboBoxCourse.addItem("Γεωμετρία");
            jComboBoxCourse.addItem("Γλώσσα");
            jComboBoxCourse.addItem("Ελένη");
            jComboBoxCourse.addItem("Ιστορία");
            jComboBoxCourse.addItem("Κείμενα Λογοτεχνίας");
            jComboBoxCourse.addItem("Μαθηματικά");
            jComboBoxCourse.addItem("Οδύσσεια");
            jComboBoxCourse.addItem("Φυσική");
            jComboBoxCourse.addItem("Χημεία");
        }

        else if( a == "Α' Λυκείου"){

            jComboBoxCourse.removeAllItems();
            // now add back relevant values
            jComboBoxCourse.addItem("Άλγεβρα");
            jComboBoxCourse.addItem("Αρχαία");
            jComboBoxCourse.addItem("Γεωμετρία");
            jComboBoxCourse.addItem("Έκθεση");
            jComboBoxCourse.addItem("Κείμενα");
            jComboBoxCourse.addItem("Φυσική");
            jComboBoxCourse.addItem("Χημεία");

        }

        else if( a == "Β' Λυκείου"){

            jComboBoxCourse.removeAllItems();
            // now add back relevant values
            jComboBoxCourse.addItem("Άλγεβρα");
            jComboBoxCourse.addItem("Αρχαία");
            jComboBoxCourse.addItem("Γεωμετρία");
            jComboBoxCourse.addItem("Έκθεση");
            jComboBoxCourse.addItem("Κείμενα");
            jComboBoxCourse.addItem("Λατινικά");
            jComboBoxCourse.addItem("Μαθηματικά Κατεύθυνσης");
            jComboBoxCourse.addItem("Φυσική Γενικής");
            jComboBoxCourse.addItem("Φυσική Κατεύθυνσης");
            jComboBoxCourse.addItem("Χημεία");
            jComboBoxCourse.addItem("Χημεία Κατεύθυνσης");

        }

        else if( a == "Γ' Λυκείου"){

            jComboBoxCourse.removeAllItems();
            // now add back relevant values
            jComboBoxCourse.addItem("Αδίδακτο");
            jComboBoxCourse.addItem("Αρχαία");
            jComboBoxCourse.addItem("Αρχές Οικονομικής Θεωρίας");
            jComboBoxCourse.addItem("Βιολογία");
            jComboBoxCourse.addItem("Ιστορία");
            jComboBoxCourse.addItem("Έκθεση");
            jComboBoxCourse.addItem("Κείμενα");
            jComboBoxCourse.addItem("Λατινικά");
            jComboBoxCourse.addItem("Μαθηματικά Κατεύθυνσης");
            jComboBoxCourse.addItem("Μαθηματικά Στατιστική");
            jComboBoxCourse.addItem("Φυσική Γενικής");
            jComboBoxCourse.addItem("Φυσική Κατεύθυνσης");
            jComboBoxCourse.addItem("Φιλοσοφικός Λόγος");
            jComboBoxCourse.addItem("Χημεία");
            jComboBoxCourse.addItem("Χημεία Κατεύθυνσης");

        }
    }//GEN-LAST:event_jComboBoxClassActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(InsertClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(InsertClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(InsertClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(InsertClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InsertClass().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExitInsertClassForm;
    private javax.swing.JButton jButtonInsertData;
    private javax.swing.JComboBox jComboBoxClass;
    private javax.swing.JComboBox jComboBoxCourse;
    private javax.swing.JComboBox jComboBoxDay;
    private javax.swing.JComboBox jComboBoxLastTime;
    private javax.swing.JComboBox jComboBoxStartTime;
    private javax.swing.JLabel jLabelClass;
    private javax.swing.JLabel jLabelCourse;
    private javax.swing.JLabel jLabelDash;
    private javax.swing.JLabel jLabelDate;
    private javax.swing.JLabel jLabelNumOfStudents;
    private javax.swing.JLabel jLabelTeacher;
    private javax.swing.JLabel jLabelTime;
    private javax.swing.JLabel jLabelTitle;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextFieldStudentNum;
    private javax.swing.JTextField jTextFieldTeacherName;
    // End of variables declaration//GEN-END:variables
}
